/**************************************
 * 作者  bush 1564049340@qq.com
 * 描述  用于将html的请求传递给实际应用程序，然后将请求结果回传给html
 *       使用本地socket完成此项功能
 **************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <net/route.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include "cgic.h"

//本地Socket位置
#define SOCK_PATH "/root/server_socket"

/**
 * @brief 生成表单文件(Sip配置)
 * @param write_buffer 发送缓冲区
 */
void makeFormDataSipConfig(char *write_buffer)
{
    /* html表单数据 */
    char html_form_value[100];

    memset(write_buffer, 0x00, sizeof(write_buffer));
    strcat(write_buffer, "HTML_SET_CONFIG:");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setSipAccount", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setSipAccount=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setSipPasswd", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setSipPasswd=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setSipDesc", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setSipDesc=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setSipServerIP", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setSipServerIP=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setSipServerPort", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setSipServerPort=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormResultType ret = cgiFormCheckboxSingle("setSipEnable");
    if(ret == cgiFormSuccess) {
        strcat(write_buffer, "setSipEnable=1");
    }
    else {
        strcat(write_buffer, "setSipEnable=0");
    }
    strcat(write_buffer, html_form_value);
}

/**
 * @brief 生成表单文件(音频配置)
 * @param write_buffer 发送缓冲区
 */
void makeFormDataAudioConfig(char *write_buffer)
{
    /* html表单数据 */
    char html_form_value[100];

    memset(write_buffer, 0x00, sizeof(write_buffer));
    strcat(write_buffer, "HTML_SET_CONFIG:");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setAudioInEnable", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setAudioInEnable=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setAiVolume", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setAiVolume=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setAoVolume", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setAoVolume=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setHpfStopFreq", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setHpfStopFreq=");
    strcat(write_buffer, html_form_value);
}

/**
 * @brief 生成表单文件(Rtp多播配置)
 * @param write_buffer  发送缓冲区
 */
void makeFormDataRtpMultiCastConfig(char *write_buffer)
{
    /* html表单数据 */
    char html_form_value[100];

    memset(write_buffer, 0x00, sizeof(write_buffer));
    strcat(write_buffer, "HTML_SET_CONFIG:");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpUrl1", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpUrl1=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpPort1", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpPort1=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpUrl2", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpUrl2=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpPort2", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpPort2=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpUrl3", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpUrl3=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpPort3", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpPort3=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpUrl4", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpUrl4=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpPort4", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpPort4=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpUrl5", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpUrl5=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpPort5", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpPort5=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpUrl6", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpUrl6=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setRtpPort6", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setRtpPort6=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&"); 
}

/**
 * @brief 生成表单文件(网络配置)
 * @param write_buffer 发送缓冲区
 */
void makeFormDataNetConfig(char *write_buffer)
{
    /* html表单数据 */
    char html_form_value[100];

    memset(write_buffer, 0x00, sizeof(write_buffer));
    strcat(write_buffer, "HTML_SET_CONFIG:");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setIPAddr", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setIPAddr=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setNetMask", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setNetMask=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");

    memset(html_form_value, 0x00, sizeof(html_form_value));
    cgiFormString("setGatewayIP", html_form_value, sizeof(html_form_value));
    strcat(write_buffer, "setGatewayIP=");
    strcat(write_buffer, html_form_value);
    strcat(write_buffer, "&");
}

int cgiMain()
{
    /* html 请求方法 */
    char *request_method = NULL;
    /* html 用户输入 */
    char *user_input = NULL;
    /* 本地套接字 */
    int sock_fd = -1;
    /* 本地套接字读缓冲区 */
    char sock_read_buffer[1024];
    /* 本地套接字写缓冲区 */
    char sock_write_buffer[1024];
    /* 本地套接字地址长度 */
    int sock_addr_len;
    /* html请求页名称 */
    char html_name[100];
    /* html文件位置 */
    char html_file_path[100];
    /* html页面源码缓存 */
    char html_content[20 * 1024];
    /* html页面文件 */
    FILE *html_file = NULL;
    /* 操作返回值 */
    int ret;

    /* 配置本地socket */
    sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    struct sockaddr_un sock_addr;
    sock_addr.sun_family = AF_UNIX;
    strcpy(sock_addr.sun_path, SOCK_PATH);
    sock_addr_len = sizeof(sock_addr);
    ret = connect(sock_fd, (struct sockaddr *)&sock_addr, sock_addr_len);
    /* 套接字打开失败 */
    if (ret == -1)
    {
        exit(1);
    }

    /* html请求包头 如果不加会出现 502 bad gateway */
    cgiHeaderContentType("text/html");
    /* 获取请求类型 */
    request_method = getenv("REQUEST_METHOD");
    if (strcmp(request_method, "GET") == 0)
    {
        /* 获取用户自定义请求类型 */
        user_input = getenv("QUERY_STRING");
        /* 发往应用程序 处理请求 */
        memset(sock_write_buffer, 0x00, sizeof(sock_write_buffer));
        strncpy(sock_write_buffer, user_input, 1024);
        write(sock_fd, sock_write_buffer, strlen(sock_write_buffer));
        read(sock_fd, sock_read_buffer, sizeof(sock_read_buffer));
        /* 收到处理结果 发往html */
        fprintf(cgiOut, sock_read_buffer);
        close(sock_fd);
    }
    else if (strcmp(request_method, "POST") == 0)
    {
        /* 获取POST请求地址 并响应 */
        cgiFormString("HtmlName", html_name, sizeof(html_name));
        /* sip配置页面 */
        if (strcmp(html_name, "sipconfig") == 0)
        {
            makeFormDataSipConfig(sock_write_buffer);
            write(sock_fd, sock_write_buffer, strlen(sock_write_buffer));
        }
        /* 音频配置页面 */
        else if (strcmp(html_name, "audioconfig") == 0)
        {
            makeFormDataAudioConfig(sock_write_buffer);
            write(sock_fd, sock_write_buffer, strlen(sock_write_buffer));
        }
        /* Rtp多播配置页面 */
        else if(strcmp(html_name,"rtpmulticastconfig") == 0) 
        {
            makeFormDataRtpMultiCastConfig(sock_write_buffer);
            write(sock_fd,sock_write_buffer,strlen(sock_write_buffer));
        }
        /* 网络配置页面 */
        else if(strcmp(html_name,"netconfig") == 0) 
        {
            makeFormDataNetConfig(sock_write_buffer);
            write(sock_fd,sock_write_buffer,strlen(sock_write_buffer));
        }
        sprintf(html_file_path, "../www/%s.html", html_name);
        html_file = fopen(html_file_path, "r");
        fread(html_content, 1, sizeof(html_content), html_file);
        fclose(html_file);
        fprintf(cgiOut, html_content);

        close(sock_fd);
    }

    return 0;
}